package com.yyks.es.dubbo.service.hotel;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.common.utils.FormatUtils;
import com.yyks.es.dubbo.pojo.param.export.EsHotelCitySearchParam;
import com.yyks.es.dubbo.pojo.param.export.EsHotelRoomCitySearchParam;
import com.yyks.es.dubbo.pojo.result.hotel.EsHotelInfoVo;
import com.yyks.es.dubbo.pojo.result.hotel.EsRoomInfoVo;
import com.yyks.es.dubbo.pojo.result.hotel.ExportHotelResult;
import com.yyks.es.dubbo.pojo.result.hotel.ExportHotelRoomResult;
import com.yyks.es.dubbo.service.MySearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author: dagny
 * @date: 2019/10/21 0021 15:57
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class EsExportHotelServiceImpl implements EsExportHotelService {

    private final MySearchService mySearchService;

    /**
     * 根据地址名称得到酒店相关的信息
     *
     * @param addressName 地址名称
     * @return 返回导出数据
     */
    @Override
    public List<ExportHotelResult> getHotelListResultInCnBooking(String addressName) {
        EsHotelCitySearchParam param = new EsHotelCitySearchParam();
        param.setAddress(addressName);
        param.setDateForPrice(DateUtil.beginOfDay(new Date()).toJdkDate());
        param.setPageSize(10000);
        List<EsHotelInfoVo> esHotelInfoVos = mySearchService.searchHotelByAddress(param);
        List<ExportHotelResult> exportHotelResults = Lists.newArrayList();
        String dateStr = DateUtils.dateToyyyyMMdd(new Date());
        for (EsHotelInfoVo esHotelInfoVo : esHotelInfoVos) {
            try {
                for (EsRoomInfoVo esRoomInfoVo : esHotelInfoVo.getEsRoomInfoVos()) {
                    ExportHotelResult result = new ExportHotelResult();
                    result.setSourceId(esHotelInfoVo.getSourceId());
                    result.setHotelName(esHotelInfoVo.getName());
                    result.setRoomName(esRoomInfoVo.getTitle());
                    Integer originPrice = esRoomInfoVo.getEsRoomCalendarInfos().get(0).getOriginPrice();
                    result.setPrice(FormatUtils.fenToYuan(originPrice));
                    result.setAddress(esHotelInfoVo.getAddress());
                    result.setDate(dateStr);
                    exportHotelResults.add(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return exportHotelResults;
    }

    /**
     * 导出某个城市的所有酒店房态
     *
     * @param cityName 城市名称
     * @return 返回结果
     */
    @Override
    public List<ExportHotelRoomResult> getHotelRoomListResultInCnBooking(String cityName) {
        EsHotelRoomCitySearchParam param = new EsHotelRoomCitySearchParam();
        param.setCityName(cityName);
        param.setPageSize(10000);
        List<EsHotelInfoVo> esHotelInfoVos = mySearchService.searchHotelByCityName(param);
        List<ExportHotelRoomResult> exportHotelRoomResults = Lists.newArrayList();
        for (EsHotelInfoVo esHotelInfoVo : esHotelInfoVos) {
            try {
                for (EsRoomInfoVo esRoomInfoVo : esHotelInfoVo.getEsRoomInfoVos()) {
                    ExportHotelRoomResult result = new ExportHotelRoomResult();
                    result.setCityId(esHotelInfoVo.getCityId());
                    result.setSourceId(esHotelInfoVo.getSourceId());
                    result.setHotelName(esHotelInfoVo.getName());
                    result.setRoomName(esRoomInfoVo.getTitle());
                    result.setCnPrice(String.valueOf(esRoomInfoVo.getOrigPrice() / 100));
                    result.setBottomPrice(String.valueOf(esRoomInfoVo.getBottomPrice() / 100));
                    result.setCurPrice(String.valueOf(esRoomInfoVo.getCurPrice() / 100));
                    exportHotelRoomResults.add(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return exportHotelRoomResults;

    }
}
